
package com.wangzha.business.service;

import com.jfinal.plugin.activerecord.Db;
import com.jfinal.plugin.activerecord.Model;
import com.jfinal.plugin.activerecord.Record;
import com.wangzha.common.base.service.BaseService;
import com.wangzha.common.kit.SqlKit;
import com.wangzha.common.model.Device;
import com.wangzha.common.vo.Grid;
import org.apache.commons.lang3.StringUtils;

import java.util.ArrayList;
import java.util.List;

/**
 * 设备列表
 */
public class DeviceService extends BaseService {
   private Device dao = new Device().dao();

   @Override
   public Model<Device> getDao(){
    	return dao;
   }

   public Grid page(int pageNumber, int pageSize, Record record) {
      Record rd = new Record();
      //rd.set("user_name like", record.getStr("searchName"));
      return queryForList(pageNumber, pageSize,rd);
   }

   public int findCountByUserCode(String userCode) {
      return Db.queryInt("select count(*) from device where user_code=?", userCode);
   }

   public Device findByActivationCode(String activationCode) {
      return dao.findFirst("select * from device where activation_code = ?", activationCode);
   }

   public List<Device> findByUserCodeAndGroupId(String userCode, Long groupId) {
      return dao.find("select * from device where user_code = ? and group_id = ?", userCode, groupId);
   }

   public List<Device> findByUserCodeAndGroupIds(String userCode, List<Long> groupIds) {
      return dao.find("select * from device where user_code = ? and group_id in " + SqlKit.joinId(groupIds), userCode);
   }

   public List<Record> queryByUserCodeAndGroupId(String userCode, Long groupId) {
      String sql = "select * from device where user_code=? " ;
      List<Object> params = new ArrayList<>();
      params.add(userCode);
      if (groupId != null && groupId != 0) {
         sql += " and group_id=?";
         params.add(groupId);
      }
      return queryForList(sql, params.toArray());
   }

   public List<Device> findByDeviceIds(List<Long> deviceIds) {
      StringBuffer sbf = new StringBuffer();
      for (Long deviceId : deviceIds) {
         if (sbf.length() > 0) {
            sbf.append(",");
         }
         sbf.append("'").append(deviceId).append("'");
      }
      return dao.find("select * from device where id in (" + sbf.toString() + ")");
   }

   public Grid queryForListByParam(int pageNumber, int pageSize, Long groupId, String activationCode,
                            String deviceName, String userCode, Integer type) {
      String sql = "select * from device where 1=1 ";
      StringBuilder whereSql = new StringBuilder();
      List<Object> params = new ArrayList<>();
      if (groupId != null) {
         whereSql.append(" and group_id = ?");
         params.add(groupId);
      }

      if (StringUtils.isNotBlank(activationCode)) {
         whereSql.append(" and activation_code = ?");
         params.add(activationCode);
      }

      if (StringUtils.isNotBlank(deviceName)) {
         whereSql.append(" and name like ?");
         params.add("%" +deviceName + "%");
      }

      if (StringUtils.isNotBlank(userCode)) {
         whereSql.append(" and user_code = ?");
         params.add(userCode);
      }

      if (type != null) {
         whereSql.append(" and type = ?");
         params.add(type);
      }

      sql += whereSql.toString();
      return queryForList(pageNumber, pageSize, sql, params.toArray());
   }
}

